package entity

import (
	"context"
	"time"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/util/structure"
	"github.com/jinzhu/gorm"
)

// GetLikeDB 获取like存储
func GetLikeDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return GetDBWithModel(ctx, defDB, new(Like))
}

// SchemaLike like对象
type SchemaLike schema.Like

// ToLike 转换为like实体
func (a SchemaLike) ToLike() *Like {
	item := new(Like)
	structure.Copy(a, item)
	return item
}

// Like like实体
type Like struct {
	ID          string     `gorm:"column:id;primary_key;size:36;"`
	VideoId     string     `gorm:"size:36;index;column:video_id;"` // 内容ID
	Type        int        `gorm:"size:11;column:type;"`           // 类型(1:点赞 2:收藏)
	ContentType int        `gorm:"column:content_type;size:11;"`   // 内容类型(1:语音 2:视频)
	Creator     string     `gorm:"column:creator;size:36;index;"`  // 创建者
	CreatedAt   time.Time  `gorm:"column:created_at;index;"`
	UpdatedAt   time.Time  `gorm:"column:updated_at;index;"`
	DeletedAt   *time.Time `gorm:"column:deleted_at;index;"`
}

// ToSchemaLike 转换为like对象
func (a Like) ToSchemaLike() *schema.Like {
	item := new(schema.Like)
	structure.Copy(a, item)
	return item
}

// Likes like列表
type Likes []*Like

// ToSchemaLikes 转换为like对象列表
func (a Likes) ToSchemaLikes() []*schema.Like {
	list := make([]*schema.Like, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaLike()
	}
	return list
}
